
package com.eshore.ezone.webservice;

import com.eshore.ezone.utils.HttpManager;
import com.eshore.ezone.utils.IOUtils;
import com.eshore.ezone.utils.LogUtils;
import com.eshore.ezone.utils.MobileConfigs;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

public class ServiceParser {
    private final static LogUtils mLog = LogUtils.getLog(ServiceParser.class);

    private ServiceParser() {
    }// Don't expect to call the constructor function.

    public static String doRequest(Context context, String method, String url,
            ArrayList<BasicNameValuePair> valuePairs, String servletPath) throws IOException {
        final HttpPost post = new HttpPost(url + servletPath);
        post.setHeader("Appstore-IMSI", MobileConfigs.getImsi(context));
        post.setHeader("Appstore-clientType", "android");
        post.setEntity(new UrlEncodedFormEntity(valuePairs));
        HttpEntity entity = null;
        try {
            final HttpResponse response = HttpManager.execute(context, post);
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                entity = response.getEntity();
                final InputStream in = entity.getContent();
                return IOUtils.stream2String(in);
            } else {
                mLog.error("StatusCode: " + response.getStatusLine().getStatusCode());
            }
        } finally {
            if (entity != null) {
                entity.consumeContent();
            }
        }
        return null;
    }

    public static String doRequest(Context context, String method,
            ArrayList<BasicNameValuePair> valuePairs, String servletPath) throws IOException {
        return doRequest(context, method, ServiceConfigs.SERVER_API_URL, valuePairs, servletPath);
    }
    
    public static String doRequest(Context context,ArrayList<BasicNameValuePair> valuePairs)throws IOException {
        
        return doRequest(context,"",ServiceConfigs.SERVER_HUAWEI_URL,valuePairs,"");
    }
    
    public static HashMap<String, String> parse2Map(String jsonString) throws IOException {
        HashMap<String, String> hashMap = null;
        try {
            mLog.info(jsonString);
            hashMap = new HashMap<String, String>();
            JSONObject jobj = new JSONObject(jsonString);
            if (jobj.has("results")) {
                jobj = jobj.optJSONObject("results");
            }

            @SuppressWarnings("unchecked")
            final Iterator<String> keyIter = jobj.keys();
            String key, value;
            while (keyIter != null && keyIter.hasNext()) {
                key = (String) keyIter.next();
                value = jobj.optString(key);
                hashMap.put(key, value);
            }
        } catch (JSONException e) {
            IOException ioe = new IOException("Json parse to map error");
            ioe.initCause(e);
            throw ioe;
        }
        return hashMap;
    }
    
    @SuppressWarnings("unchecked")
    public static HashMap<String, String> parse2Map(JSONObject jsonObj) throws IOException {
        final HashMap<String, String> hashMap = new HashMap<String, String>();;
        final Iterator<String> keyIter = jsonObj.keys();
        String key, value;
        while (keyIter != null && keyIter.hasNext()) {
            key = (String) keyIter.next();
            value = jsonObj.optString(key);
            hashMap.put(key, value);
        }
        return hashMap;
    }

    public static ArrayList<HashMap<String, String>> parse2List(JSONArray jsonArr) throws IOException {
        ArrayList<HashMap<String, String>> arrList = null;
        arrList = new ArrayList<HashMap<String, String>>();
        final int len = jsonArr.length();
        for (int i = 0; i < len; i++) {
            final JSONObject jobj = (JSONObject) jsonArr.opt(i);
            arrList.add(parse2Map(jobj));
        }
        return arrList;
    }
}
